package com.jnpc.spark.project.utils;

import com.jnpc.spark.BatchProcessDataOnYARN2;
import org.neo4j.driver.v1.*;
import static org.neo4j.driver.v1.Values.parameters;

/**
 * Created by yangqiang on 2018/4/2 0002.
 */
public class CRProcess {
    private static volatile Driver driver;
    public CRProcess(){
        driver= getInstance();
    }

    public static Driver getInstance() {
        if(driver == null){
            synchronized(CRProcess.class){
                if(driver == null){
                    driver =  GraphDatabase.driver("bolt://127.0.0.1:7687",
                            AuthTokens.basic("neo4j", "123456"));
                }
            }
        }
        return driver;
    }

    public void CreateCRreport(BatchProcessDataOnYARN2.CRReport cr)
    {

        try (Session session = driver.session())
        {
            try (Transaction tx = session.beginTransaction())
            {
                tx.run("Merge (a:状态报告 {name: {x1},id:{x2},desc: {x3},cause: {x4}})",
                        parameters("x1", cr.cr_subject(),
                                "x2", cr.id_key_cr(),
                                "x3", cr.cr_desc(),
                                "x4", cr.cr_cause()));
                tx.success();  // Mark this write as successful.
            }
            CreateKKS(cr.cr_equipment());
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:状态报告 {id:{x1}}),(b:KKS {id:{x2}}) " +
                        "Merge (b)-[:产生状态报告]->(a)";
                tx.run( s,
                        parameters("x1", cr.id_key_cr(),
                                "x2", cr.cr_equipment() ));
                tx.success();  // Mark this write as successful.
            }
        }
    }

    public void CreateLOERREPORT(BatchProcessDataOnYARN2.LOERREPORT loerreport )
    {

        try (Session session = driver.session())
        {
            try (Transaction tx = session.beginTransaction())
            {
                tx.run("Merge (a:运行事件报告 {loerno: {x1},idkey:{x2},subject: {x3} })",
                        parameters("x1", loerreport.loer_no(),
                                "x2", loerreport.id_key_cr(),
                                "x3", loerreport.t_subject() ));
                tx.success();  // Mark this write as successful.
            }
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:状态报告 {id:{x1}}),(b:运行事件报告 {idkey:{x2}}) " +
                        "where a.id=b.idkey " +
                        "Merge (b)-[:关联状态报告]->(a)";
                tx.run( s,
                        parameters("x1", loerreport.id_key_cr(),
                                "x2", loerreport.id_key_cr() ));
                tx.success();
            }
        }
    }
    public void CreateIERCAPCNNP(BatchProcessDataOnYARN2.IER_CAP_CNNP ier_cap_cnnp )
    {

        try (Session session = driver.session())
        {
            try (Transaction tx = session.beginTransaction())
            {
                tx.run("Merge (a:内部事件报告 {er_no: {x1},idkey:{x2},er_name: {x3},desc:{x4} })",
                        parameters("x1", ier_cap_cnnp.er_no(),
                                "x2", ier_cap_cnnp.id_key_cr(),
                                "x3", ier_cap_cnnp.t_er_name(),
                                "x4", ier_cap_cnnp.t_er_desc()
                        ));
                tx.success();  // Mark this write as successful.
            }
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:状态报告 {id:{x1}}),(b:内部事件报告 {idkey:{x2}}) " +
                        "where a.id=b.idkey " +
                        "Merge (b)-[:关联状态报告]->(a)";
                tx.run( s,
                        parameters("x1", ier_cap_cnnp.id_key_cr(),
                                "x2", ier_cap_cnnp.id_key_cr() ));
                tx.success();
            }
        }
    }

    public void CreateCAList(BatchProcessDataOnYARN2.CA_LIST_CNNP ca )
    {

        try (Session session = driver.session())
        {
            try (Transaction tx = session.beginTransaction())
            {
                tx.run("Merge (a:纠正行动 {cano: {x1},idkey:{x2},desc: {x3} })",
                        parameters("x1", ca.ca_no(),
                                "x2", ca.id_key_cr(),
                                "x3", ca.t_ca_desc() ));
                tx.success();
            }
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:状态报告 {id:{x1}}),(b:纠正行动 {idkey:{x2}}) " +
                        "where a.id=b.idkey " +
                        "Merge (a)-[:关联纠正行动]->(b)";
                tx.run( s,
                        parameters("x1", ca.id_key_cr(),
                                "x2", ca.id_key_cr() ));
                tx.success();  // Mark this write as successful.
            }
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:内部事件报告 {idkey:{x1}}),(b:纠正行动 {idkey:{x2}}) " +
                        "where a.idkey=b.idkey " +
                        "Merge (a)-[:关联纠正行动]->(b)";
                tx.run( s,
                        parameters("x1", ca.id_key_cr(),
                                "x2", ca.id_key_cr() ));
                tx.success();  // Mark this write as successful.
            }
            try (Transaction tx = session.beginTransaction())
            {
                String s="Match (a:运行事件报告 {idkey:{x1}}),(b:纠正行动 {idkey:{x2}}) " +
                        "where a.idkey=b.idkey " +
                        "Merge (a)-[:关联纠正行动]->(b)";
                tx.run( s,
                        parameters("x1", ca.id_key_cr(),
                                "x2", ca.id_key_cr() ));
                tx.success();  // Mark this write as successful.
            }
        }
    }

    public void CreateKKS(String kks)
    {

        try (Session session = driver.session())
        {
            try (Transaction tx = session.beginTransaction())
            {
                tx.run("Merge (a:KKS {id: {x1}})",
                        parameters("x1", kks ));
                tx.success();
            }
        }
    }
    public void close()
    {
        // Closing a driver immediately shuts down all open connections.
        driver.close();
    }

    public static void main(String... args)
    {
      //  String str="CRTEMP2017008242@CR341704646@30LCT10AA505@30@2017-04-11@19@30LCT10AA505阀门内漏@2017年4月11日18:00，三号机常规岛现场操作员在31UMA08米巡检时发现30LCT10AA505阀门内漏，五秒一滴，地面积水两平方米。@30LCT10AA505阀门内漏。";
    /*    CRProcess example = new CRProcess("bolt://127.0.0.1:7687", "neo4j", "123456");

         example.CreateCRreport(str);

        //example.CreateKKS("30LCT10AA505");
        example.close();*/
    }
}
